from sqlalchemy import Column, Integer, String, Date, Float, ForeignKey
from sqlalchemy.orm import relationship
from .session import Base

class StockDaily(Base):
    __tablename__ = 'stock_daily'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    trade_date = Column(Date, nullable=False)
    stock_code = Column(String(6), nullable=False)
    stock_name = Column(String(50), nullable=False)
    industry = Column(String(30))
    close = Column(Float, nullable=False)
    change_pct = Column(Float, nullable=False)
    market_cap = Column(Float, nullable=False)
    volume = Column(Integer)
    
    # 兼容性别名
    @property
    def date(self):
        return self.trade_date
        
    @property
    def code(self):
        return self.stock_code
    
    def __repr__(self):
        return f"<StockDaily({self.stock_code} {self.trade_date})>"

class IndexDaily(Base):
    __tablename__ = 'index_daily'
    
    id = Column(Integer, primary_key=True)
    trade_date = Column(Date, nullable=False)
    index_code = Column(String(6), nullable=False)
    index_name = Column(String(50), nullable=False)
    close = Column(Float, nullable=False)
    change_pct = Column(Float, nullable=False)
    volume = Column(Integer)
    
    # 兼容性别名
    @property
    def date(self):
        return self.trade_date
        
    @property
    def code(self):
        return self.index_code
    
    def __repr__(self):
        return f"<IndexDaily({self.index_code} {self.trade_date})>"

class StockInfo(Base):
    __tablename__ = 'stock_info'
    
    id = Column(Integer, primary_key=True)
    code = Column(String(6), nullable=False, unique=True)
    name = Column(String(50), nullable=False)
    industry = Column(String(30))
    market = Column(String(10))  # 市场：上海、深圳、北京
    list_date = Column(Date)     # 上市日期
    
    def __repr__(self):
        return f"<StockInfo({self.code} {self.name})>"